<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\index\controller;

use app\common\controller\Base;
use utils\Aes\Aes5;
use utils\Aes\Aes7;
use utils\IPIP\IP;

/**
 * @title 所有人的页面
 */
class Everyone extends Base {

    public function login() {


        $this->assign('seo', [
            'title' => '用户登录',
            'keywords' => '',
            'description' => '',
        ]);


        if (request()->isPost()) {

            $username = input('post.username');
            $password = input('post.password');

            if ($username && $password) {


                $result = db('member')
                        ->where('username', $username)
                        ->where('password', my_md5($password))
                        ->field('id,username,password,nickname,avatar')
                        ->find();

                if ($result) {

                    $result_json = json_encode($result);

                    // PHP5
                    // $result_json_aes = Aes5::encrypt($result_json, substr(md5('nariims'), 8, 16));
                    // PHP7
                    $obj = new Aes7([
                        'key' => APP_THEME
                    ]);

                    cookie('token', $obj->aesEn($result_json));

                    // 审计
                    $data['ip'] = $_SERVER['HTTP_X_REAL_IP'] ?? $_SERVER['REMOTE_ADDR'];
                    $data['address'] = implode('-', array_filter(IP::find($data['ip'])));
                    $data['create_time'] = time();
                    $data['mid'] = $result['id'];

                    db('member_login_log')->insert($data);

                    $this->success('登录成功', 'index/member/index');
                } else {
                    $this->error('用户不存在或密码有误');
                }
            } else {
                $this->error('用户名或密码不能为空');
            }
        } else {

            return view();
        }
    }

    /**
     * @title 注册一个新用户
     */
    public function reg() {


        if (request()->isPost()) {


            $username = input('post.username');
            $password = input('post.password');

            if ($username && $password) {


                //
                $username_count = db('member')->where('username', $username)->count();
                if ($username_count) {
                    $this->error('用户名已经存在，不能注册');
                }

                //
                $insert_id = db('member')->insertGetId([
                    'username' => $username,
                    'password' => my_md5($password),
                    'create_time' => time()
                ]);


                if ($insert_id) {
                    $this->success('注册成功', 'index/everyone/login');
                } else {
                    $this->error('注册失败');
                }

                //
            } else {
                $this->error('用户名或密码不能为空');
            }
        } else {

            return view();
        }
    }

    /**
     * @title 退出
     */
    public function logout() {

        cookie('token', NULL);

        $this->success('', url('everyone/login'));
    }

}
